<!DOCTYPE html>
<html class="x-admin-sm">
<head>
    <meta charset="UTF-8">
    <title>查询顾客信息</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="stylesheet" href="/css/font.css">
    <link rel="stylesheet" href="/css/xadmin.css">
    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <script type="text/javascript" src="/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="/js/xadmin.js"></script>
    <script type="text/javascript" src="/js/cookie.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>

<body>
<div class="x-nav">
      <span class="layui-breadcrumb">
        <a href="">首页</a>
        <a href="">演示</a>
        <a>
          <cite>导航元素</cite></a>
      </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
       href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon" style="line-height:30px">ဂ</i></a>
</div>
<div class="x-body">
    <div class="layui-row">
        <input type="hidden" name="sex" id="sex">
        <input type="hidden" name="checkId" id="checkId">
        <form class="layui-form layui-col-md12  layui-form-pane" id="search">
            <div class="layui-form-item">
                <div class="layui-input-inline">
                    <input type="tel" name="phone" id="phone" placeholder="联系方式" autocomplete="off"
                           class="layui-input">
                </div>
                <div class="layui-input-inline">
                    <input type="text" name="name" id="name" placeholder="姓名" autocomplete="off"
                           class="layui-input">
                </div>
                <button type="button" class="layui-btn" lay-submit lay-filter="search"><i class="layui-icon">&#xe615;</i></button>
            </div>
        </form>
        <table class="layui-table" id="r_table" lay-filter="test">
        </table>
        <div class="page" id="page">

        </div>
    </div>
</div>

<script>
    window.onload = function () {
        //获取表格中指定列的值传递给新修改弹窗
        layui.use(['layer', 'table', 'form', 'jquery'], function () {
            var $ = layui.jquery,
                layer = layui.layer,
                form = layui.form;
            var table = layui.table;
            var laypage = layui.laypage;
            //根据返回的房间类型设置下拉框中房间类型的值
            var roomType;
            // $.get({
            //     url: '/customer/list',
            //     dataType: 'json',
            //     success: function (res) {
            //         roomType = res.data;
            //         var selectType = $("#roomType");
            //         roomType.forEach(function (ele, index) {
            //             var option = $('<option></option>');
            //             //如果该类型在下拉框中没有，则添加
            //             if ($('#roomType option[value=' + ele['id'] + ']').length == 0) {
            //                 option.val(ele['id']);
            //                 option.text(ele['roomType']);
            //                 option.appendTo(selectType);
            //             }
            //             form.render('select');
            //         });
            //
            //     }
            // });
            //渲染表格
            var tableIns = table.render({
                id: 'tableId'
                , elem: '#r_table'
                , height: 400
                , url: '/customer/list' //数据接口
                , toolbar: '#toolbarDemo'
                , parseData: function (res) {
                    console.log(res);
                    var data = res.data;
                    //执行一个laypage实例
                   /* laypage.render({
                        elem: 'page' //注意，这里的 test1 是 ID，不用加 # 号
                        ,count: res.count//数据总数，从服务端得到
                    });*/
                    //处理状态，转编号为文字
                    for (var index in data) {
                        switch (data[index]["sex"]) {
                            case 1:
                                data[index]["sex"] = "男";
                                break;
                            case 0:
                                data[index]["sex"] = "女";
                                break;
                        }
                    }
                    return {
                        "code": res.status, //解析接口状态
                        "msg": res.msg, //解析提示文本
                        "count": res.total, //解析数据长度
                        "data": data //解析数据列表
                    }
                }
                , page:true
                ,limit:20
                , cols: [
                    [ //表头
                        {type: 'checkbox',width:"5%", fixed: 'left'},
                        {field: 'id', title: 'ID', width: '8%', sort: true, fixed: 'left', align: 'center'}
                        , {field: 'phone', title: '联系方式', width: '12%', sort:true, align: 'center'}
                        , {field: 'name', title: '姓名', width: '8%', align: 'center'}
                        , {field: 'sex', title: '性别', width: '10%', align: 'center'}
                        , {field: 'customerCard', title:'身证件号', width: '14%', align: 'center'}
                        , {field: 'age', title: '性别', width: '7%', align: 'center'}
                        ,{field: 'checkId', title: '入住订单号', width: '10%', align: 'center'}
                        ,{field: 'remark', title: '备注', width: '11%', align: 'center'}
                        , {field: 'customerEdit', title: '操作', toolbar: '#edit', width: '17%', align: 'center'}
                    ]
                ]
                , done: function (res, curr, count) {
                    table.on('tool(test)', function (obj) {
                        //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
                        //console.log(obj);
                        var data = obj.data; //获得当前行数据
                        var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
                        switch (data["sex"]) {
                            case "男":
                                delStatus = 1;
                                break;
                            case "女":
                                delStatus = 0;
                                break;
                        }
                        //当要触发修改事件的时候，传递data数据
                        if (layEvent == "edit") {
                            $('#checkId').val(data["checkId"]);
                            $('#sex').val(data["sex"]);
                            editRoom('修改', '/customer/customerEdit', 500, 400, data);
                        }
                        else if (layEvent === 'del') { //删除
                            layer.confirm('确定删除吗？', function (index) {
                                $.post("/customer/remove", {'ids': data.id}, function (res) {
                                    console.log(obj);
                                    if (res.status == 0) {
                                        obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                                        layer.alert("删除成功", {icon: 6})
                                    }
                                    else {
                                        layer.alert("删除失败", {icon: 5})
                                    }
                                });
                                layer.close(index);
                                //向服务端发送删除指令
                            });
                        }
                    });
                }

            });
            form.on('submit(search)', function (data){
                console.log("search data",data);
                tableIns.reload({
                    where:data.field
                });
            });

            //修改房间信息弹窗
            function editRoom(title, url, w, h, data) {
                //data为选中行的房间信息
                if (title == null || title == '') {
                    title = false;
                }
                ;
                if (url == null || url == '') {
                    url = "404.html";
                }
                ;
                if (w == null || w == '') {
                    w = ($(window).width() * 0.9);
                }
                ;
                if (h == null || h == '') {
                    h = ($(window).height() - 50);
                }
                ;
                var index = layer.open({
                    type: 2,
                    area: [w + 'px', h + 'px'],
                    fix: false, //不固定
                    maxmin: true,
                    shadeClose: true,
                    shade: 0.4,
                    title: title,
                    content: url,
                    success: function (layero, index) {
                        //在新窗口保留之前的值，在有值的基础上修改,body为子窗口
                        var body = layer.getChildFrame('body', index);
                        //表格渲染后获取指定行数据
                        body.find("#id").val(data["id"]);
                        body.find("#phone").val(data["phone"]);
                        body.find("#name").val(data["name"]);
                        body.find("#remark").val(data["remark"]);
                        body.find("#age").val(data["age"]);
                        body.find("#customerCard").val(data["customerCard"]);
                    }
                });
            }
            table.on("toolbar(test)",function (obj) {
                console.log(obj);
                var checkStatus = table.checkStatus(obj.config.id);
                if(obj.event=='delAll'){
                    layer.confirm('确定删除吗？',function (index) {
                        var arr=[];
                        console.log(checkStatus);
                        $.each(checkStatus.data, function (idx, obj) {
                            //获得所有id
                            arr.push(obj.id);
                        });
                        // 异步操作
                        var result = 0;
                        console.log(arr);
                        $.each(arr,function (index,ele) {
                            $.post("/customer/remove", {'ids': ele}, function (res) {
                                console.log("res",res);
                                if (res.status!=0)
                                    result=1;
                            });
                        })
                        if (result == 0) {
                            layer.msg("删除成功", {icon: 6})
                            tableIns.reload();
                        }
                        else {
                            layer.alert("删除失败", {icon: 5})
                        }
                        layer.close(index);
                    })
                }
                else if(obj.event=='addUser'){
                    add("增添顾客", 400, 400, "customer/customerAdd");
                }
            });

            //增加房间信息弹窗
            function add(title, w, h, url, roomType) {
                if (title == null || title == '') {
                    title = false;
                }
                ;
                if (url == null || url == '') {
                    url = "404.html";
                }
                ;
                if (w == null || w == '') {
                    w = ($(window).width() * 0.9);
                }
                ;
                if (h == null || h == '') {
                    h = ($(window).height() - 50);
                }
                ;
                layer.open({
                    type: 2,
                    area: [w + "px", h + "px"],
                    fix: false,
                    shade: 0.3,
                    title: title,
                    shadeClose: true,//点击遮罩层关闭
                    content: url,
                    success: function (layero, index) {
                        var body = layer.getChildFrame('body', index);

                    },
                    end: function () {
                        //层被销毁后刷新页面
                        location.reload();
                    }

                });
            }
        });
    };

</script>
<script type="text/html" id="edit">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<!--头部工具栏-->
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-danger" lay-event="delAll"><i class="layui-icon"></i>批量删除</button>
        <button class="layui-btn" lay-event="addUser">添加顾客</button>

    </div>
</script>
</body>

</html>